package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Observable;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.fim.core.service.impl.LoginServiceImpl;


public class LoginForm extends AbstractForm {

	private static final long serialVersionUID = -5226475637443523307L;
	
	private JTextField userTextField;
	private JTextField passwordTextField;
	
	public LoginForm(String titulo) {
		super(titulo);
		initialize();
		update(null, null);
	}

	public void update(Observable arg0, Object arg1) {
		pack();
	}

	protected void initializeDynamicComponents() {
		initializeUserTextField();
		initializePasswordTextField();
	}
	
	private void initializeUserTextField() {
		userTextField = new JTextField();
	}
	
	private void initializePasswordTextField() {
		passwordTextField = new JPasswordField();
	}

	protected void buildLayout() {
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		Container inputContainer = createInputContainer();
		Container buttonContainer = createButtonContainer();
		
		getContentPane().add(inputContainer);
		getContentPane().add(buttonContainer);
	}
	
	private Container createInputContainer() {
		Component userLabel = new JLabel("User:");
		Component passwordLabel = new JLabel("Password:");
		
		Container container = new Container();
		GroupLayout layout = new GroupLayout(container);
		container.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(userLabel)
						.addComponent(passwordLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(userTextField)
						.addComponent(passwordTextField)));
		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(userLabel)
						.addComponent(userTextField))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(passwordLabel)
						.addComponent(passwordTextField)));
		return container;
	}
	
	private Container createButtonContainer() {
		Container container = new Container();
		container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
		
		// Component registerComponent = createRegisterComponent();
		Component loginComponent = createLoginComponent();
		Component exitComponent = createExitComponent();
		
		// container.add(registerComponent);
		container.add(loginComponent);
		container.add(exitComponent);
		
		return container;
	}
	
	private Component createRegisterComponent() {
		AbstractButton registerComponent = new JButton("Register");
		registerComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				registerComponentActionPerformed();
			}
		});
		return registerComponent;
	}
	
	private void registerComponentActionPerformed() {
		// TODO: registerComponentActionPerformed
	}
	
	private Component createLoginComponent() {
		AbstractButton loginComponent = new JButton("Login");
		loginComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				loginComponentActionPerformed();
			}
		});
		return loginComponent;
	}
	
	private Component createExitComponent() {
		AbstractButton exitComponent = new JButton("Exit");
		exitComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});
		return exitComponent;
	}
	
	private void loginComponentActionPerformed() {
		LoginServiceImpl loginServiceImpl = new LoginServiceImpl();
		try {
			loginServiceImpl.login(userTextField.getText(), passwordTextField.getText());
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
	}
}